﻿using UnityEngine;
using System.Collections;

public class Fire : MonoBehaviour
{

    private Light m_Light;

    [SerializeField]
    private float m_ChangeRate;
    [SerializeField]
    private float m_ChangeAmount;
    [SerializeField]
    private float m_MaxChange;

    private float m_NowRate;
    private float m_NowAmount;
    private float m_Max;
    private float m_Min;
    // Use this for initialization
    void Start()
    {
        m_Light = GetComponent<Light>();
        m_Max = m_Light.intensity * (1 + m_MaxChange);
        m_Min = m_Light.intensity * (1 - m_MaxChange);
    }

    void Update()
    {
        m_NowRate = Random.Range(0f, 100f);
        if (m_NowRate < m_ChangeRate)
        {
            m_NowAmount = Random.Range(-m_ChangeAmount, m_ChangeAmount);
            ChangeLight(m_NowAmount);
        }
    }

    public void ChangeLight(float amount)
    {
        if (m_Light.intensity * (1 + amount) > m_Max || m_Light.intensity < m_Min)
        {
            return;
        }
        m_Light.intensity *= 1 + amount;
        m_Light.range *= 1 + amount / 2;
    }
}
